package com.sinopec.handdevice.view

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.jakewharton.rxbinding3.view.clicks
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.sinopec.basemodule.adapter.CommonAdapter
import com.sinopec.basemodule.adapter.CustomViewHolder
import com.sinopec.basemodule.base.BaseActivity
import com.sinopec.basemodule.base.CustomToolbar
import com.sinopec.basemodule.launchcontract.UHFDailyInventoryResultLaunch
import com.sinopec.basemodule.model.PackSpecsTypeModel
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.handdevice.R
import com.sinopec.handdevice.contract.DailyInventoryListContract
import com.sinopec.handdevice.presenter.DailyInventoryListPresenter
import com.sinopec.resmodule.widget.SpaceItemDecoration
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_daily_inventory_list.*
import java.util.concurrent.TimeUnit

/**
 * @author lwj
 * @ClassName: DailyInventoryListActivity
 * @Description:日常盘点-获取包装规格列表
 */
@Route(path = ARouterManagerUtils.GOTO_MAIN_DAILY_INVENTORY_ACTIVITY)
class DailyInventoryListActivity : BaseActivity<DailyInventoryListPresenter>(),
    DailyInventoryListContract.View {
    private var datas = arrayListOf<PackSpecsTypeModel>()
    private var adapter: CommonAdapter<PackSpecsTypeModel>? = null
    private var linearLayoutManager: LinearLayoutManager? = null

    //包装规格
    private var chooseModel: PackSpecsTypeModel = PackSpecsTypeModel()

    /**
     * 日常盘库扫码启动器
     */
    private val uhfDailyInventoryLauncher by lazy {
        registerForActivityResult(UHFDailyInventoryResultLaunch) {
            //如果需要关闭当前界面
//            if (it != null && it) {
//                finish()
//            }
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun onResume() {
        super.onResume()
        srl.autoRefresh()
    }

    override fun initEvent() {
        super.initEvent()

        /**
         * 日常盘库扫码启动器
         */
        uhfDailyInventoryLauncher

        /**
         * 确定按钮
         */
        addDisposable(tv_confirm.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (chooseModel.packSpecsTypeId == null) {
                    showMessage("请选择包装规格！")
                    return@subscribe
                }
                uhfDailyInventoryLauncher.launch(chooseModel)
            }
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        mPresenter = DailyInventoryListPresenter(this)
        linearLayoutManager = LinearLayoutManager(mContext)
        rv_list.layoutManager = linearLayoutManager
        rv_list.addItemDecoration(SpaceItemDecoration(0, 0))
        srl.setEnableLoadMore(false)
        //srl.autoRefresh()

        //刷新控件
        srl.setOnRefreshListener { refreshlayout: RefreshLayout ->
            mPresenter.dailyInventoryList()
            refreshlayout.finishRefresh()
        }
        srl.setOnLoadMoreListener { refreshlayout: RefreshLayout ->
            mPresenter.dailyInventoryList()
            refreshlayout.finishLoadMore()
        }

        adapter = object :
            CommonAdapter<PackSpecsTypeModel>(mContext, R.layout.item_daily_inventory_list, datas) {
            override fun convert(
                holder: CustomViewHolder,
                model: PackSpecsTypeModel,
                position: Int,
            ) {
                val itemView = holder.itemView
                //item
                val ll_item = holder.getView<LinearLayout>(R.id.ll_item)
                //图标
                val ic_select = holder.getView<ImageView>(R.id.ic_select)
                if (model.packSpecsTypeId == chooseModel.packSpecsTypeId) {
                    //ll_item.background = resources.getDrawable(R.drawable.rectangle_f7f8fa)
                    ll_item.background = resources.getDrawable(R.drawable.rectangle_6e85f9_t_10dr)
                    ic_select.visibility = View.VISIBLE
                } else {
                    //ll_item.background = resources.getDrawable(R.drawable.rectangle_ffffff)
                    ll_item.background = resources.getDrawable(R.drawable.rectangle_ffffff_10dr)
                    ic_select.visibility = View.GONE
                }

                //包装规格类型
                val tv_packSpecsTypeName = holder.getView<TextView>(R.id.tv_packSpecsTypeName)
                tv_packSpecsTypeName.text = "${model.packSpecsTypeName}"

                /**
                 *  选择
                 */
                addDisposable(itemView.clicks()
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe {
                        chooseModel = model
                        adapter?.notifyDataSetChanged()
                    })
            }
        }
        rv_list.adapter = adapter
    }

    override fun getContentView(): Int {
        return R.layout.activity_daily_inventory_list
    }

    override fun configToolbar(rToolbar: CustomToolbar) {
        //配置标题
        rToolbar.setTitle("请选择包装规格")
    }

    override fun refreshUI(list: List<PackSpecsTypeModel>) {
        //如果已选中的规格不存在列表中就重置
        if (!list.isNullOrEmpty() && !list.map { it.packSpecsTypeId }.contains(chooseModel.packSpecsTypeId)) {
            chooseModel = PackSpecsTypeModel()
        }
        datas.clear()
        datas.addAll(list)
        adapter?.notifyDataSetChanged()
    }
}